﻿#include "handle_manage.h"

handle_manage::handle_manage()
{
    qRegisterMetaType<emit_type>("emit_type");
}

handle_manage::~handle_manage()
{
    QMap<Handle*,QThread*>::const_iterator i;
    for (i = thread_map.constBegin(); i != thread_map.constEnd(); ++i) {    //对QMAP进行遍历
        i.key()->deleteLater();
        i.value()->quit();
        i.value()->wait();
        i.value()->deleteLater();
    }
    qDebug()<<"handle_manage已关闭!";
}

void handle_manage::setHandleCount(int handleNum)
{
    this->handle_count=handleNum;
}

void handle_manage::init()
{
    for (int a=0;a<this->handle_count;a++){
        QThread   *thread=new QThread();
        Handle   *handle=new Handle(a);
        thread_map[handle]=thread;     //设置线程map
        thread_task_num[handle]=0;     //设置线程任务数
        connect(this,SIGNAL(Handlelocaldata(QString,QString,QString,local_socket*,Handle*,emit_type)),handle,SLOT(handle_localSocket(QString,QString,QString,local_socket*,Handle*,emit_type)),Qt::QueuedConnection);
        connect(this,SIGNAL(Handletcpdata(QString,QString,QString,tcp_socket*,Handle*,emit_type)),handle,SLOT(handle_tcpSocket(QString,QString,QString,tcp_socket*,Handle*,emit_type)),Qt::QueuedConnection);
        connect(handle,SIGNAL(l_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),this,SLOT(Cl_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),Qt::QueuedConnection);
        connect(handle,SIGNAL(t_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),this,SLOT(Ct_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),Qt::QueuedConnection);
        handle->moveToThread(thread);
        thread->start();
    }
    //QSharedPointer<Handle> handle= Choice_Free_Handle();

}


//获取负载较少的处理线程
Handle *handle_manage::Choice_Free_Handle()
{
    int thread_min_cout=INT_MAX;
    Handle *handle=NULL;
    QMap  <Handle*,int>::const_iterator i;
    for (i = thread_task_num.constBegin(); i != thread_task_num.constEnd(); ++i) {    //对QMAP进行遍历
        if(i.value()<thread_min_cout)//选择负载最小线程
        {
            thread_min_cout=i.value();
            handle=i.key();
        }
    }
    return handle;
}


void handle_manage::receive(QString id, QString type, QString data)
{

    Handle *handle=Choice_Free_Handle();
    if (handle==NULL)
    {
        qDebug()<<"获取到的处理线程异常！！！";
    }
    ++thread_task_num[handle];    //任务数+1
    if (local_socket* l_socket = dynamic_cast<local_socket*>(sender())){
        emit Handlelocaldata(id,type,data,l_socket,handle,One);
    }
    else if (tcp_socket *t_socket = dynamic_cast<tcp_socket*>(sender())){
        emit Handletcpdata(id,type,data,t_socket,handle,One);
    }
}

void handle_manage::Cl_SocketSendData(QString id, QString type, QString data, local_socket *l_socket, emit_type te_ype,QString socketType)
{
    emit this->l_SocketSendData(id,type,data,l_socket,te_ype,socketType);
}

void handle_manage::Ct_SocketSendData(QString id, QString type, QString data, tcp_socket *t_socket, emit_type te_ype, QString socketType)
{
    emit this->t_SocketSendData(id,type,data,t_socket,te_ype,socketType);
}

